package com.yc.tomcat.impl;

import com.yc.tomcat.Cookie;
import com.yc.tomcat.HttpServletRequest;
import com.yc.tomcat.HttpServletResponse;
import com.yc.tomcat.HttpSession;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpServletResponseImpl implements HttpServletResponse {

    private Socket socket;
    private HttpServletRequest request;
    private int status = 200;
    private String msg = "OK";
    private Map<String, String> headerMap = new HashMap<>();
    private List<Cookie> cookieList = new ArrayList<>();

    public HttpServletResponseImpl(Socket socket,
                                   HttpServletRequest request) {
        this.socket = socket;
        this.request = request;
    }

    // 字符串输出流 => 资源流
    StringWriter sw = new StringWriter();
    // 打印输出流 => 处理流
    PrintWriter pw = new PrintWriter(sw);
    @Override
    public PrintWriter getWriter() {
        return pw;
    }

    @Override
    public OutputStream getOutputStream() {
        try {
            return socket.getOutputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void setContentType(String type) {
        setHeader("Content-Type", type);
    }

    @Override
    public void setStatus(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    @Override
    public void setHeader(String name, String value) {
        headerMap.put(name, value);
    }

    @Override
    public void flushBuffer() {
        try {
            OutputStream out = socket.getOutputStream();
            // 头行
            out.write(String.format("%s %d %s\n",
                    request.getProtocol(), status, msg).getBytes());
            // 头部参数
            for (Map.Entry<String, String> entry : headerMap.entrySet()) {
                String name = entry.getKey();
                String value = entry.getValue();
                out.write(String.format("%s: %s\n",
                        name, value).getBytes());
            }
            // 保存會話ID到Cookie中
            HttpSession session = request.getSession();
            String sessionId = session.getId();
            Cookie sessionCookie = new CookieImpl("JSESSIONID", sessionId);
            cookieList.add(sessionCookie);

            // Cookie
            for (Cookie cookie : cookieList) {
                out.write(("Set-Cookie: "+ cookie.toString()).getBytes());
            }
            // 空行
            out.write("\n".getBytes());
            // body 内容
            out.write(sw.getBuffer().toString().getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void sendRedirect(String location) {
        // 相应结果码： 301
        setStatus(301, "Redirect");
        // 相应头参数： Location
        setHeader("Location", location);
    }

    @Override
    public void addCookie(Cookie cookie) {
        cookieList.add(cookie);
    }
}
